package com.peak.distribution.entity.agent;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;

import com.peak.distribution.enums.Constants;
import com.peak.spring.boot.api.entity.DefaultStategyEntity;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Entity
@DynamicUpdate
@Table(name = "dis_agent_sales")
@Data
@EqualsAndHashCode(callSuper=true)
public class DisAgentSales extends DefaultStategyEntity {

	private static final long serialVersionUID = Constants.DISTRIBUTION_VERSION;

	/** 本日销量 */
	private Integer daySales;

	/** 本周销量 */
	private Integer weekSales;

	/** 本月销量 */
	private Integer monthSales;

	/** 本季度销量 */
	private Integer quarterSales;

	/** 总销量 */
	private Integer totalSales;
	
	/** 总销售商品数量 */
	private Integer totalGoodsCount;

	/** 代理商个人 */
	private DisAgent agent;

	/** 团队 */
	private DisAgentTeam team;
	
	/** 签名 */
	private String sign;

	@ManyToOne
	@JoinColumn(name = "agent_id")
	@Fetch(FetchMode.SELECT)
	@LazyToOne(LazyToOneOption.PROXY)
	public DisAgent getAgent() {
		return agent;
	}

	@ManyToOne
	@JoinColumn(name = "team_id")
	@Fetch(FetchMode.SELECT)
	@LazyToOne(LazyToOneOption.PROXY)
	public DisAgentTeam getTeam() {
		return team;
	}
	
	@Transient
	public String getSignSource() {
		String id = this.agent == null ? "null" : this.agent.getId().toString();
		String teamId = this.team == null ? "null" : this.team.getId().toString();
		return id + teamId + this.daySales + this.monthSales + this.quarterSales + this.totalSales + this.weekSales + this.totalGoodsCount;
	}
	
	public void addSales(Integer daySales) {
		this.daySales = this.daySales == null ? daySales  : this.daySales + daySales;
	}
	
	public void addTotalSales(Integer daySales) {
		this.totalSales = this.totalSales == null ? daySales  : this.totalSales + daySales;
	}

	public void addSalesGoods(int sum) {
		this.totalGoodsCount = this.totalGoodsCount == null ? sum  : this.totalGoodsCount + sum;
	}
}
